//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "VarianceOption.h"
using namespace Cephei::QL::Experimental::Varianceoption;
#include <gen/QL/Payoff.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instrument.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Varianceoption::CVarianceOption::CVarianceOption (Cephei::QL::IPayoff^ payoff, Double notional, DateTime startDate, DateTime maturityDate, Cephei::QL::IPricingEngine^ QL_Pricer) : CInstrument(CVarianceOption::typeid)
{
    CPayoff^ _Cpayoff;
    try
    {
#ifdef HANDLE
        _phVarianceOption = NULL;
#endif
        _Cpayoff = safe_cast<CPayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::Payoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::Payoff>&> (_Cpayoff->GetShared ()); 
        QuantLib::Real _notional = (QuantLib::Real)ValueHelper::Convert (notional); //d
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate); //d
        QuantLib::Date _maturityDate = (QuantLib::Date)ValueHelper::Convert (maturityDate); //d
        _ppVarianceOption = new boost::shared_ptr<QuantLib::VarianceOption> (new QuantLib::VarianceOption ( _payoff,  _notional,  _startDate,  _maturityDate ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppVarianceOption)->setPricingEngine (_QL_Pricer);
        SetInstrument (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppVarianceOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
    }
}
Cephei::QL::Experimental::Varianceoption::CVarianceOption::CVarianceOption (boost::shared_ptr<QuantLib::VarianceOption>& childNative, Object^ owner) : CInstrument(CVarianceOption::typeid)
{
#ifdef HANDLE
	_phVarianceOption = NULL;
#endif
	_ppVarianceOption = &childNative;
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppVarianceOption));
}
Cephei::QL::Experimental::Varianceoption::CVarianceOption::CVarianceOption (QuantLib::VarianceOption& childNative, Object^ owner) : CInstrument(CVarianceOption::typeid)
{
#ifdef HANDLE
	_phVarianceOption = NULL;
#endif
	_ppVarianceOption = new boost::shared_ptr<QuantLib::VarianceOption> (&childNative);
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppVarianceOption));
    _VarianceOptionOwner = owner;
    _InstrumentOwner = owner;
}

Cephei::QL::Experimental::Varianceoption::CVarianceOption::CVarianceOption (CVarianceOption^ copy) : CInstrument(CVarianceOption::typeid)
{
#ifdef HANDLE
	_phVarianceOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppVarianceOption = new boost::shared_ptr<QuantLib::VarianceOption> (copy->GetShared());
        _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppVarianceOption));
    }
}
Cephei::QL::Experimental::Varianceoption::CVarianceOption::CVarianceOption (PLATFORM::Type^ t) : CInstrument(CVarianceOption::typeid)
{
#ifdef HANDLE
	_phVarianceOption = NULL;
#endif
	if (!t->IsSubclassOf(CVarianceOption::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Varianceoption::CVarianceOption::CVarianceOption (QuantLib::Handle<QuantLib::VarianceOption>& childNative, Object^ owner)  : CInstrument(CVarianceOption::typeid)
{
	_phVarianceOption = &childNative;
	_ppVarianceOption = &static_cast<boost::shared_ptr<QuantLib::VarianceOption>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppVarianceOption));
    _VarianceOptionOwner = owner;
}
Cephei::QL::Experimental::Varianceoption::CVarianceOption::CVarianceOption (QuantLib::Handle<QuantLib::VarianceOption> childNative)  : CInstrument(CVarianceOption::typeid)
{
	_phVarianceOption = &childNative;
	_ppVarianceOption = &static_cast<boost::shared_ptr<QuantLib::VarianceOption>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppVarianceOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Varianceoption::CVarianceOption::CVarianceOption (QuantLib::VarianceOption childNative)  : CInstrument(CVarianceOption::typeid)
{
#ifdef HANDLE
	_phVarianceOption = NULL;
#endif
	_ppVarianceOption = new boost::shared_ptr<QuantLib::VarianceOption> (new QuantLib::VarianceOption (childNative));
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppVarianceOption));
}
#endif

Cephei::QL::Experimental::Varianceoption::CVarianceOption::~CVarianceOption ()
{
    if (_ppVarianceOption != NULL)
    {
	    delete _ppVarianceOption;
        _ppVarianceOption = NULL;
    }
}
Cephei::QL::Experimental::Varianceoption::CVarianceOption::!CVarianceOption ()
{
    if (_ppVarianceOption != NULL)
    {
	    delete _ppVarianceOption;
    }
}
QuantLib::VarianceOption& Cephei::QL::Experimental::Varianceoption::CVarianceOption::GetReference ()
{
    if (_ppVarianceOption == NULL) throw REFNEW NativeNullException ();
	return **_ppVarianceOption;
}
boost::shared_ptr<QuantLib::VarianceOption>& Cephei::QL::Experimental::Varianceoption::CVarianceOption::GetShared ()
{
    if (_ppVarianceOption == NULL) throw REFNEW NativeNullException ();
	return *_ppVarianceOption;
}
QuantLib::VarianceOption* Cephei::QL::Experimental::Varianceoption::CVarianceOption::GetPointer ()
{
    if (_ppVarianceOption == NULL) throw REFNEW NativeNullException ();
	return &**_ppVarianceOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::VarianceOption>& Cephei::QL::Experimental::Varianceoption::CVarianceOption::GetHandle ()
{
	if (_phVarianceOption == NULL)
	{
		_phVarianceOption = new Handle<QuantLib::VarianceOption> (*_ppVarianceOption);
	}
	return *_phVarianceOption;
}
#endif
bool Cephei::QL::Experimental::Varianceoption::CVarianceOption::HasNative () 
{
	return (_ppVarianceOption != NULL);
}

Boolean Cephei::QL::Experimental::Varianceoption::CVarianceOption::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppVarianceOption)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Varianceoption::CVarianceOption::MaturityDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppVarianceOption)->maturityDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Varianceoption::CVarianceOption::Notional::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppVarianceOption)->notional ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IPayoff^ Cephei::QL::Experimental::Varianceoption::CVarianceOption::Payoff::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::Payoff> _rv = (boost::shared_ptr<QuantLib::Payoff>)(*_ppVarianceOption)->payoff ( );   
        Cephei::QL::CPayoff^ _nrv = REFNEW Cephei::QL::CPayoff (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Varianceoption::CVarianceOption::StartDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppVarianceOption)->startDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Varianceoption::IVarianceOption^ Cephei::QL::Experimental::Varianceoption::CVarianceOption_Factory::Create (Cephei::QL::IPayoff^ payoff, Double notional, DateTime startDate, DateTime maturityDate, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CVarianceOption ( payoff,  notional,  startDate,  maturityDate,  QL_Pricer);
}
